﻿<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<fmt:setBundle basename="ApplicationMessage" />
<fmt:setLocale value="zh_CN" />

<sec:authentication property="principal" var="currentPrincipal" scope="page" />

<div class="modal-dialog x-modal-dialog" style="width: 700px; margin-top: 50px;">

	<div class="modal-content">

		<div class="modal-header x-breadcrumb no-bottom-border clearfix">

			<h2>
				<i class="glyphicon glyphicon-fire" title="创建订单"></i>&nbsp;&nbsp;新增渠道记录
			</h2>

			<button type="button" class="close" data-dismiss="modal">&times;</button>

		</div>

		<form class="form-horizontal" method="POST" action="${ctx.host}/user-source/${userSource.id}/edit" data-validate="true">


			<input type="hidden" name="id" value="${userSource.id}">

			<div class="modal-body">
				<div class="form-group x-form-group">
					<label class="col-xs-3 control-label">用户手机号</label>
					<div class="pull-left col-xs-3">
						<input name="mobile" type="text" class="form-control" value="${userSource.mobile}">
					</div>
					<label class="col-xs-2 control-label">用户姓名</label>
					<div class="pull-left col-xs-3">
						<input name="name" type="text" class="form-control" value="${userSource.name}">
					</div>
				</div>

				<div class="form-group x-form-group">
					<label class="col-xs-3 control-label">性别</label>
					<div class="pull-left col-xs-3">
						<select name="gender" class="form-control" data-validate="required">
							<option value=""></option>
							<option value="男" ${userSource.gender == '男' ? 'selected' : ''}>男</option>
							<option value="女" ${userSource.gender == '女' ? 'selected' : ''}>女</option>
						</select>
					</div>
					<label class="col-xs-2 control-label">微信号</label>
					<div class="pull-left col-xs-3">
						<input name="wechatnumber" type="text" class="form-control" value="${userSource.wechatnumber}">
					</div>
				</div>

				<c:if test="${currentPrincipal.root or currentPrincipal.admin or currentPrincipal.areaServer or currentPrincipal.areaManager}">
					<div class="form-group x-form-group">
						<label class="col-xs-3 control-label">接待人</label>
						<div class="pull-left col-xs-3">
							<select name="receiverId" class="form-control">
								<option value=""></option>
								<c:forEach var="principal" items="${persist.principals}">
									<c:if test="${!principal.disabled}">
										<option value="${principal.id}" ${userSource.receiver.id eq principal.id ? 'selected' : ''}>${principal.profile.realname}</option>
									</c:if>
								</c:forEach>
							</select>
						</div>
						<label class="col-xs-2 control-label">接待日期</label>
            <div class="pull-left col-xs-3">
              <input type="text" class="form-control date" name="occurDate" data-validate="required" placeholder="请选择日期" readOnly value='<fmt:formatDate value="${userSource.occurDate}" pattern="yyyy-MM-dd"/>' />
            </div>
					</div>
				</c:if>

				<c:if test="${currentPrincipal.siteManager or currentPrincipal.siteServer}">
					<div class="form-group x-form-group">
						<label class="col-xs-3 control-label">接待人</label>
						<div class="pull-left col-xs-3">
							<select name="receiverId" class="form-control">
								<option value=""></option>
								<c:forEach var="principal" items="${persist.getPrincipals(currentPrincipal.site.id)}">
									<c:if test="${!principal.disabled}">
										<option value="${principal.id}" ${userSource.receiver.id eq principal.id ? 'selected' : ''}>${principal.profile.realname}</option>
									</c:if>
								</c:forEach>
							</select>
						</div>
						<label class="col-xs-2 control-label">接待日期</label>
            <div class="pull-left col-xs-3">
              <input type="text" class="form-control date" name="occurDate" data-validate="required" placeholder="请选择日期" readOnly value='<fmt:formatDate value="${userSource.occurDate}" pattern="yyyy-MM-dd"/>' />
            </div>
					</div>
				</c:if>


				<div class="form-group x-form-group">
					<label class="col-xs-3 control-label">渠道</label>
					<div class="pull-left col-xs-8">
						<c:set value="${persist.listUserSourceTypeParent()}" var="typeParent"></c:set>
						<c:set value="${persist.listUserSourceTypeChildren()}" var="typeChildrenMap"></c:set>
						<select name="userSourceTypeId" class="form-control" data-validate="required">

							<c:forEach var="typeParentItem" items="${typeParent}">
								<optgroup label="${typeParentItem.name}">
									<c:choose>
										<c:when test="${not empty typeChildrenMap[typeParentItem.id.toString()]}">
											<c:forEach var="childrenTypes" items="${typeChildrenMap[typeParentItem.id.toString()]}">
												<c:if test="${currentPrincipal.siteManager or currentPrincipal.siteServer}">
													<c:choose>
														<c:when test="${not empty childrenTypes.sites}">
															<c:if test="${childrenTypes.sites.contains(currentPrincipal.site.code)}">
																<option value="${childrenTypes.id}" ${userSource.userSourceType.id eq childrenTypes.id ? 'selected' : ''}>${typeParentItem.name}-${childrenTypes.name}</option>
															</c:if>
														</c:when>
														<c:otherwise>
															<option value="${childrenTypes.id}" ${userSource.userSourceType.id eq childrenTypes.id ? 'selected' : ''}>${typeParentItem.name}-${childrenTypes.name}</option>
														</c:otherwise>
													</c:choose>
												</c:if>
												<c:if test="${currentPrincipal.root or currentPrincipal.admin or currentPrincipal.areaServer or currentPrincipal.areaManager}">
													<option value="${childrenTypes.id}" ${userSource.userSourceType.id eq childrenTypes.id ? 'selected' : ''}>${typeParentItem.name}-${childrenTypes.name}</option>
												</c:if>

											</c:forEach>
										</c:when>
										<c:otherwise>
											<option value="${typeParentItem.id}" ${userSource.userSourceType.id eq typeParentItem.id ? 'selected' : ''}>${typeParentItem.name}</option>
										</c:otherwise>
									</c:choose>
								</optgroup>
							</c:forEach>
						</select>
					</div>
				</div>

				<div class="form-group x-form-group">
					<label class="col-xs-3 control-label">学员需求</label>
					<div class="pull-left col-xs-8">
						<input name="requirement" type="text" class="form-control" value="${userSource.requirement}">
					</div>
				</div>


			</div>

			<div class="modal-footer clearfix">
				<button type="submit" class="btn btn-success btn-shadow btn-shadow-success x-btn">确定</button>
				&nbsp;&nbsp;&nbsp;&nbsp;
				<button type="button" class="btn btn-default btn-shadow btn-shadow-default x-btn" data-dismiss="modal">取消</button>
			</div>
		</form>

	</div>

</div>

<script src="${ctx.resource}/js/component/bootstrap.validate.js"></script>
<script src="${ctx.resource}/js/component/bootstrap.validate.en.js"></script>
<script src="${ctx.resource}/js/bootstrap.validate.trigger.js"></script>
<script>
$(document).ready(function(){
  
  $('select[name="gender"]').select2({
        placeholder : '请选择性别',
        minimumResultsForSearch : 5,
        allowClear : true,
      });
  

  
  $('.date').datetimepicker({
    format: 'yyyy-mm-dd',
    autoclose: true,
    language: 'zh-CN',
    todayBtn: 'linked',
    todayHighlight: true,
    pickerPosition: 'bottom-right',
    minView: 'month'
  });
    
  
  var sourceType = $('select[name="userSourceTypeId"]').select2({
      placeholder : '请选择渠道',
      minimumResultsForSearch : 5,
      allowClear : true,
    });
  
  $('select[name="userSourceTypeId"]').on("change", function(e) { 
    console.log("change "+JSON.stringify({val:e.val, added:e.added, removed:e.removed}));
    if(e.val == '0') {
      $("#addNew").hasClass("hide") ? $("#addNew").removeClass("hide") : null;
    } else {
      $("#addNew").hasClass("hide") ? null : $("#addNew").addClass("hide");
    }
    })
  
  $('select[name="receiverId"]').select2({
      placeholder : '请选择接待人',
      minimumResultsForSearch : 5,
      allowClear : true,
    });
  
  
  $('form').shiftform({
    beforeSubmit : function(el) {
      $('form').trigger("validate.ajax.submit");
      return $.bt_validate.result[$.bt_validate.form_id]
    },
    failure: function(code, e, bg, mg, url) {
    	  window.location.reload();
    },
  });
});
</script>
